package com.gr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.entity.*;
import com.gr.service.IBrandService;
import com.gr.service.ICategoryBrandRelationService;
import com.gr.service.ICategoryService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guoran
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;
    @Autowired
    private ICategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ICategoryService categoryService;
    @GetMapping("/selectBrandByCategoryId/{categoryId}")//不改,是前端传,测试时用params传就行
    public ResultEntity<IPage<Brand>> selectBrandByCategoryId(@PathVariable("categoryId") Long categoryId, int pageNo, int pageSize){//页号页宽都不能写死，前端elementui会改变
        IPage<Brand> page=new Page<>(pageNo,pageSize);
        QueryWrapper<CategoryBrandRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        List<CategoryBrandRelation> list = categoryBrandRelationService.list(wrapper);
        //用stream映射出品牌id集合
        List<Long> brandIdList = list.stream().map((cbr) -> {
            return cbr.getBrandId();
        }).collect(Collectors.toList());
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.in("brand_id",brandIdList);//wrapper调mybatisplus枚举方法
        IPage<Brand> brandList = brandService.page(page,wrapper1);//不能调list，调page
        return ResultEntity.ok(brandList);
    }

    @PostMapping("/add")
    public ResultEntity add(@RequestBody Brand brand){
        brandService.save(brand);
        return ResultEntity.ok();
    }

    /**
     * postman测试 用body中form-data 下拉框传参
     * @param headImg
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadImg")
    public ResultEntity<String> upload(MultipartFile headImg) throws IOException {
        headImg.transferTo(new File(path,headImg.getOriginalFilename()));
        return ResultEntity.ok(path);
    }

    @DeleteMapping("/delete/{brandId}")
    public ResultEntity delete(@PathVariable("brandId")Integer brandId){
        brandService.removeById(brandId);
        return ResultEntity.ok();
    }

    @GetMapping("/selectOne/{brandId}")
    public ResultEntity<Brand> selectOne(@PathVariable("brandId")Integer brandId){
        QueryWrapper<Brand> wrapper=new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        Brand one = brandService.getOne(wrapper);
        return ResultEntity.ok(one);
    }

    @PutMapping("/update")
    public ResultEntity update(@RequestBody Brand brand){
        brandService.updateById(brand);
        return ResultEntity.ok();
    }

    @PostMapping("/relaCate")
    public ResultEntity relaCate(@RequestBody CategoryBrandDto dto){
        QueryWrapper<Brand> wrapper=new QueryWrapper();
        wrapper.eq("brand_id",dto.getBrandId());
        Brand brand = brandService.getOne(wrapper);//先拿到需要关联商品类别的品牌，需要属性：id、name
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.in("category_id",dto.getCateIds());
        List<Category> list = categoryService.list(wrapper1);//根据类别id数组查询类别
        //类别需要的属性：id、name
        List<CategoryBrandRelation> cbrList=new ArrayList<>();
        //遍历类别集合，完成赋值
        for (Category category:list){
            CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
            categoryBrandRelation.setBrandId(dto.getBrandId());
            categoryBrandRelation.setBrandName(brand.getName());
            categoryBrandRelation.setCategoryId(category.getCategoryId());
            categoryBrandRelation.setCategoryName(category.getCategoryName());
            //怎么赋值？
            cbrList.add(categoryBrandRelation);
            categoryBrandRelationService.save(categoryBrandRelation);//数据入库
        }
        return ResultEntity.ok();
    }
}


